def interpolate_color(color1, color2, ratio):
  """在两个颜色之间进行插值"""
  r = int(color1[0] + (color2[0] - color1[0]) * ratio)
  g = int(color1[1] + (color2[1] - color1[1]) * ratio)
  b = int(color1[2] + (color2[2] - color1[2]) * ratio)
  return r, g, b


def get_gradient_color(p, colors):
  if p < 0.0 or p > 1.0:
    raise ValueError("p must be between 0.0 and 1.0")
  if len(colors) < 2:
    raise ValueError("At least two colors are required to create a gradient")

  # 计算 `p` 所在的区间
  num_intervals = len(colors) - 1
  interval_index = int(p * num_intervals)

  # 确定插值比例
  local_p = (p * num_intervals) - interval_index

  # 获取当前区间的两端颜色
  color1 = colors[interval_index]
  color2 = colors[min(interval_index + 1, len(colors) - 1)]

  # 进行颜色插值
  return interpolate_color(color1, color2, local_p)


def RGBToHex(rgb):
  """将 RGB 颜色元组转换为十六进制字符串"""
  return "#" + "".join([f"{int(x):02x}" for x in rgb]).upper()


s2k = {
  'keypad -': 109,
  'a': 65,
  'b': 66,
  'c': 67,
  'd': 68,
  'e': 69,
  'f': 70,
  'g': 71,
  'h': 72,
  'i': 73,
  'j': 74,
  'k': 75,
  'l': 76,
  'm': 77,
  'n': 78,
  'o': 79,
  'p': 80,
  'q': 81,
  'r': 82,
  's': 83,
  't': 84,
  'u': 85,
  'v': 86,
  'w': 87,
  'x': 88,
  'y': 89,
  'z': 90,
  '0': 48,
  '1': 49,
  '2': 50,
  '3': 51,
  '4': 52,
  '5': 53,
  '6': 54,
  '7': 55,
  '8': 56,
  '9': 57
}


def StrToKeys(s):
  if type(s) == str:
    s = s.lower()
    if '+' in s:
      data = s.split('+')
      return StrToKeys(data)
    return [s2k.get(s, s)]
  return [k if type(k) == int else s2k.get(k, k) for k in s]
